package org.itstack.command;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MacroCommand implements Command{
    private final List<Command> commands = new ArrayList<>();

    public void addCommand(Command command) {
        commands.add(command);
    }


    @Override
    public void execute() {
        System.out.println("MacroCommand execute");
        for (Command command : commands) {
            command.execute();
        }
        // TODO Auto-generated method stub
        
    }

    @Override
    public void undo() {
       System.out.println("MacroCommand undo");
        // 反向撤销
        ListIterator<Command> listIterator = commands.listIterator(commands.size());
        while (listIterator.hasPrevious()) {
            listIterator.previous().undo();
        }
    }

}
